package com.controller;

import com.api.CommonResult;
import com.entity.BType;
import com.entity.table.BGoodsTableDef;
import com.entity.table.BTypeTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.BTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.BGoods;
import com.service.BGoodsService;

import java.io.Serializable;
import java.util.List;

/**
 * 商品表 控制层。
 *
 * @author admin
 * @since 2024-05-31
 */
@Controller
@RequestMapping("/admin/good")
public class BGoodsController {

    @Autowired
    private BGoodsService bGoodsService;

    @Autowired
    private BTypeService bTypeService;

    @GetMapping(value = "/goodList")
    public String userList(ModelMap model, @RequestParam(value = "goodName", defaultValue ="")String goodName,
                           @RequestParam(value = "p", defaultValue = "1")int p,
                           @RequestParam(value = "pageSize", defaultValue = "10")int pageSize) {
        try {
            QueryWrapper queryWrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.GOOD_NAME.like(goodName));
            Page<BGoods> page = new Page<>();
            page.setPageSize(pageSize);
            page.setPageNumber(p);
            Page<BGoods> pageInfo = bGoodsService.getMapper().paginateWithRelations(page, queryWrapper);
//            String typename=pageInfo.getRecords().get(0).getBType().getTypeName();
            model.addAttribute("page", pageInfo);
            model.addAttribute("total", pageInfo.getTotalRow());
        }catch (Exception e){
            e.printStackTrace();
        }
        return "back/goods-list";
    }
    @GetMapping(value = "/addView")
    public String addView(Model model){
        List<BType> typeList=bTypeService.list();
        model.addAttribute("typeList",typeList);
        return "back/goods-add";
    }

    /**
     * 添加商品表。
     *
     * @param bGoods 商品表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ResponseBody
    public CommonResult save(BGoods bGoods) {
        try {
            boolean r = bGoodsService.save(bGoods);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bGoods.getGoodName()+"已存在";
            return CommonResult.failed(msg);
        }
    }

    /**
     * 根据主键删除商品表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return bGoodsService.removeById(id);
    }

    @GetMapping(value = "updateView")
    public String updateView(int id, ModelMap model){
        List<BType> typeList=bTypeService.list();
        BGoods bGoods = bGoodsService.getById(id);
        model.addAttribute("typeList",typeList);
        model.addAttribute("bGood",bGoods);
        return "back/goods-edit";
    }
    /**
     * 根据主键更新商品表。
     *
     * @param bGoods 商品表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update")
    @ResponseBody
    public CommonResult update( BGoods bGoods) {
        try {
            boolean r =bGoodsService.updateById(bGoods);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = bGoods.getGoodName()+"已存在";
            return CommonResult.failed(msg);
        }
    }
    @PostMapping("del")
    @ResponseBody
    public CommonResult del(Integer id){
        try{
            boolean r = bGoodsService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
    /**
     * 查询所有商品表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<BGoods> list() {
        return bGoodsService.list();
    }

    /**
     * 根据商品表主键获取详细信息。
     *
     * @param id 商品表主键
     * @return 商品表详情
     */
    @GetMapping("getInfo/{id}")
    public BGoods getInfo(@PathVariable Serializable id) {
        return bGoodsService.getById(id);
    }

    /**
     * 分页查询商品表。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<BGoods> page(Page<BGoods> page) {
        return bGoodsService.page(page);
    }

}
